import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testsecret:

    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/secret/1_get_secret_list.yaml'))
    def test_get_secret_list(self, secretinfo, get_env_info):
        # 获取secret列表接口
        baseUrl = get_env_info
        url = baseUrl + secretinfo['request']['url'].replace('{namespace}','default')
        method = secretinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/secret/2_create_secret.yaml'))
    def test_create_secret(self, secretinfo, get_env_info):
        # 创建secret接口
        baseUrl = get_env_info
        url = baseUrl + secretinfo['request']['url'].replace('{namespace}','default')
        method = secretinfo['request']['method']
        data = secretinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '-' not in secretinfo['name']:
            secretname=req.json()['metadata']['name']
            globals()['secretname']=secretname
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/secret/3_get_specific_secret.yaml'))
    def test_get_specific_secret(self, secretinfo, get_env_info):
        # 获取指定secret接口
        baseUrl = get_env_info
        url = baseUrl + secretinfo['request']['url'].replace('{namespace}','default').replace('{secretname}',globals()['secretname'])
        method = secretinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in secretinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/secret/4_update_secret.yaml'))
    def test_update_secret(self, secretinfo, get_env_info):
        # 更新指定secret接口
        baseUrl = get_env_info
        url = baseUrl + secretinfo['request']['url'].replace('{namespace}','default').replace('{secretname}',globals()['secretname'])
        method = secretinfo['request']['method']
        secretinfo['request']['data'] = globals()['updatereqbody']
        data = secretinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/secret/5_update_section_secret.yaml'))
    def test_update_section_secret(self, secretinfo, get_env_info):
        # 部分更新指定secret的接口
        baseUrl = get_env_info
        url = baseUrl + secretinfo['request']['url'].replace('{namespace}','default').replace('{secretname}',globals()['secretname'])
        method = secretinfo['request']['method']
        data = secretinfo['request']['data']
        header = secretinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('secretinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/secret/6_delete_specific_secret.yaml'))
    def test_delete_specific_secret(self, secretinfo, get_env_info):
        # 删除secret接口
        baseUrl = get_env_info
        url = baseUrl + secretinfo['request']['url'].replace('{namespace}','default').replace('{secretname}',globals()['secretname'])
        method = secretinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == secretinfo['validate'][0]['equals']['status_code']